﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-06 14:43:34
 * 功能：界面【LoadingPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using System.Collections.Generic;
using UnityEngine;

namespace Game.UI.LoadingPanel
{
    public class LoadingPanelView : GameLoadingPanel.GameLoadingPanelView
    {
        public new const string PANEL_KEY = "Game_UI_LoadingPanel_LoadingPanel_Key";
        private bool m_IsHandledAd;
        private float m_Time2;
        private bool m_IsHandledAll;
        private bool m_IsHandledNetComplate;
        private bool m_IsStartHandleNet;
        private bool m_IsPlayBgMusic;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
            EventCenter.Instance.AddEventListen<string>(GameEventName.ON_HANDLE_NOTIFY, OnHandleNotify);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
            EventCenter.Instance.RemoveEventListen<string>(GameEventName.ON_HANDLE_NOTIFY, OnHandleNotify);
        }

        public override void Init(object data)
        {
            GameDataMgr.Instance.Save();
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            NativeInteraction.Instance.BIUploadPanelOpen("loading");
        }

        protected override void AnimAfterHandle()
        {
            // 播发背景音乐
            if (FrameworkManager.Instance.CustomProgress >= 1 && !m_IsPlayBgMusic)
            {
                m_IsPlayBgMusic = true;
                AudioManager.Instance.PlayBg("Audios/Bg");
            }

            HandleNetAssets();

            if (m_LoadingProgress >= 1)
            {
                if (!m_IsHandledAd)
                    WaitOpAd();
                else if (!m_IsHandledAll)
                    LoadingComplate();
            }
        }

        // 处理网络资源
        private void HandleNetAssets()
        {
            if (FrameworkManager.Instance.CustomProgress >= 1 && !m_IsStartHandleNet)
            {
                m_IsStartHandleNet = true;
                var curLv = GameDataMgr.Instance.CurLevel;

                if (curLv == 1) // 缓存第二关卡的缩略图
                {
                    var secondLvData = GameDataMgr.Instance.GetLevel(curLv + 1);
                    if (secondLvData.assetsInfo == null)
                        AssetCache.CacheSmallImgs(secondLvData, () => m_IsHandledNetComplate = true);
                    else
                        m_IsHandledNetComplate = true;
                }
                else
                {
                    var levelData = GameDataMgr.Instance.GetLevel(curLv);
                    if (levelData.assetsInfo == null) // 没有缓存缩略图
                    {
                        var unusedCaches = GameDataMgr.Instance.GetUnusedCache();
                        levelData.assetsInfo = new List<GameDataModel.AssetInfo>();

                        foreach (var item in unusedCaches)
                        {
                            levelData.assetsInfo.Add(item.Value.Clone());
                            if (levelData.assetsInfo.Count >= 4)
                                break;
                        }

                        // 如果前面缓存的内容不够用了，就走这里处理
                        int needCount = 4 - levelData.assetsInfo.Count;
                        for (int i = 4 - needCount; i < 4; i++)
                        {
                            levelData.assetsInfo.Add(new GameDataModel.AssetInfo());
                            AssetCache.CacheSingleSmallImg(levelData, i, null);
                        }
                    }
                    else
                        m_IsHandledNetComplate = true;
                }
            }
        }

        private void WaitOpAd()
        {
            if (GameDataMgr.Instance.CurLevel > BaseDataMgr.Instance.Cfg.ads.open_c && !GameDataMgr.Instance.IsPurchased_RemoveAds())
            {
                m_Time2 += Time.deltaTime;
                if (!AdMgr.Instance.IsOpenReady() && m_Time2 < 2) return;

                if (AdMgr.Instance.IsOpenReady())
                    AdMgr.Instance.ShowOpen(ETriggerAdType.AppOpen);
            }
            m_IsHandledAd = true;
        }

        private void LoadingComplate()
        {
            m_IsHandledAll = true;
            GameDataMgr.Instance.Save();
            if (!GameDataMgr.Instance.IsNoFirstEnterGame)
            {
                UIUtility.DisplayPanel(GamePanel.GamePanelView.PANEL_KEY, "UI/GamePanel/Prefabs/GamePanel", data: GameDataMgr.Instance.GetLevel(GameDataMgr.Instance.CurLevel));
                GameDataMgr.Instance.IsNoFirstEnterGame = true;
            }
            else
                UIUtility.DisplayPanel(HallPanel.HallPanelView.PANEL_KEY, "UI/HallPanel/Prefabs/HallPanel");
            UIUtility.DestroyPanel(PANEL_KEY);

            // 检测通知
            //EventCenter.Instance.Trigger(GameEventName.CHECK_NOTIFY);

            // 创建激励接插屏过度界面
            //UIUtility.DisplayPanel(AdditionIntPanel.AdditionIntPanelView.PANEL_KEY, "UI/AdditionIntPanel/Prefabs/AdditionIntPanel", UIRoot.Top);
        }

        protected override bool WaitCondition()
        {
            return !m_IsHandledNetComplate;
        }

        private void OnHandleNotify(string jumpPath)
        {
            if (string.IsNullOrEmpty(jumpPath)) return;

            string panelMediator = "";

            //根据path跳转对应的页面
            switch (jumpPath)
            {
                case "":
                    break;
            }

            if (string.IsNullOrEmpty(panelMediator)) return;
            //BI打点
            var data = new Dictionary<string, object>
            {
                {"scene", jumpPath},
            };
            NativeInteraction.Instance.SendBILog("notification", data);

            var status = UIUtility.GetPanelState(panelMediator);
            if (status == PanelState.Show) return;

            // 打开界面
            // TODO:
        }
    }

    public class AssetItemReq
    {
        public string tag;      // 标签
        public int num;         // 数量
        public string sign;     // 签名
        public bool needNew;    // 是否需要新内容
    }
}